angular.module('payPcApp')
    .factory('user_model', ['$http', 'AppConfig', '$location', function($http, AppConfig, $location) {

        var login = function(param) {
            var url = AppConfig.WEB_ROOT + 'user/login';
            var data = 'userId=' + param.userId + '&password=' + param.password;
            console.log(data);
            return $http({
                method: 'post',
                url: url,
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
                },
                data: data
            });
        };
        var cancel = function(param) {
            var url = AppConfig.WEB_ROOT + 'user/logout/' + AppConfig.get_login_identity(); // + ;
            return $http({
                method: 'get',
                url: url
            });
        };
        var get_userinfo = function() {
            var url = AppConfig.WEB_ROOT + 'user/selectByKey';
            var data = 'employee=' + AppConfig.get_login_identity();
            return $http({
                method: 'post',
                url: url,
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
                },
                data: data
            });
        }
        var lend_history = function(param) {
            param.currentPage = param.currentPage ? param.currentPage : 1;
            var url = AppConfig.WEB_ROOT + 'bookLend/myLend/' + AppConfig.get_login_identity() + '/' + param.type + '/' + param.currentPage + '/20';
            return $http({
                method: 'post',
                url: url,
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
                }
            });
        };
        var collectABook = function(param) {
            var url = AppConfig.WEB_ROOT + 'bookShelf/collectABook/' + AppConfig.get_login_identity();
            var data = 'isbn=' + param.isbn;
            return $http({
                method: 'post',
                url: url,
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
                },
                data: data
            })
        };
        var unCollectABook = function(param) {
            var url = AppConfig.WEB_ROOT + 'bookShelf/unCollectABook/' + AppConfig.get_login_identity();
            var data = 'isbn=' + param.isbn;
            return $http({
                method: 'post',
                url: url,
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
                },
                data: data
            })
        };
        var myBookShelf = function(param) {
            param.currentPage = param.currentPage ? param.currentPage : 1;
            var url = AppConfig.WEB_ROOT + 'bookShelf/myBookShelf/' + AppConfig.get_login_identity() + '/' + param.currentPage + '/10';
            return $http({
                method: 'get',
                url: url
            });
        }
        var loanABook = function(param) {
            var url = AppConfig.WEB_ROOT + 'bookLoan/loanABook/' + AppConfig.get_login_identity() + '/' + param.isbn;
            var data = 'remarks="123"';
            return $http({
                method: 'post',
                url: url,
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
                },
                data: data
            })
        };
        var saveinfo = function(param) {
            var url = AppConfig.WEB_ROOT + 'user/updateAUser/' + AppConfig.get_login_identity();
            return $http({
                method: 'post',
                url: url,
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
                },
                data: param
            })
        };
        var myLoan = function(param) {
            param.currentPage = param.currentPage ? param.currentPage : 1;
            var url = AppConfig.WEB_ROOT + 'bookLoan/myLoan/' + AppConfig.get_login_identity() + '/' + param.currentPage + '/10';
            return $http({
                method: 'post',
                url: url,
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
                },
                data: param
            })
        };
        return {
            login: login,
            cancel: cancel,
            get_userinfo: get_userinfo,
            lend_history: lend_history,
            collectABook: collectABook,
            myBookShelf: myBookShelf,
            loanABook: loanABook,
            unCollectABook: unCollectABook,
            saveinfo: saveinfo,
            myLoan: myLoan
        };
    }])